package com.ruoyi.yl.service.impl;


import com.ruoyi.yl.domain.YlDrug;
import com.ruoyi.yl.domain.YlStockArrange;
import com.ruoyi.yl.domain.YlStockArrangeDrug;
import com.ruoyi.yl.domain.YlUpdateStock;
import com.ruoyi.yl.mapper.DrugMapper;
import com.ruoyi.yl.mapper.StockArrangeManageMapper;
import com.ruoyi.yl.service.StockArrangeManageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: linKe
 * @Date: 2022/2/19 9:29
 * @FileName: DrugInfoMaintenanceServiceImpl
 * @version: 1.0
 * @Description: 库存盘点
 */

@Service
@Transactional(readOnly = true)
public class StockArrangeManageServiceImpl implements StockArrangeManageService {

    @Resource
    private StockArrangeManageMapper stockArrangeManageMapper;
    @Resource
    private DrugMapper drugMapper;
    /**
     * 查询所有盘点信息
     */
    @Override
    public List<YlStockArrange> getAll() {
        // 关联用户查出用户id
        return stockArrangeManageMapper.selectList(null);
    }

    /**
     * 新增盘点
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    public int addStockArrange(YlStockArrange stockArrange) {
        //
        return stockArrangeManageMapper.insert(stockArrange);
    }
    /**
     * 根据id查药品和盘点
     * @param id
     * @return
     */
    @Override
    public YlStockArrangeDrug getOneById(Long id) {
        YlStockArrange ylStockArrange = stockArrangeManageMapper.selectById(id);
        List<YlDrug> allDrug = drugMapper.selectList(null);

        return new YlStockArrangeDrug().setDrugsS(allDrug).setStockArrange(ylStockArrange);
    }

    /**
     * 根据药品的id修改库存
     * @param ylUpdateStock
     */
    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    public void updateDrugStock(YlUpdateStock ylUpdateStock){
        // for循环更新  可优化
        for(int i=0;i<ylUpdateStock.getIds().size();i++){
            stockArrangeManageMapper.updateDrugStock(ylUpdateStock.getIds().get(i),ylUpdateStock.getStocks().get(i));
        }
    }
    /**
     * 根据盘点id删数据
     */
    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    public void deleteStockArrange(Long id) {
        stockArrangeManageMapper.deleteById(id);
    }

    /**
     * 根据盘点单号模糊查信息
     */
    @Override
    public List<YlStockArrange> getStockArragesByNumber(String number){

        return stockArrangeManageMapper.getStockArragesByNumber(number);
    }


    /**
     * 根据时间段查信息
     */
    @Override
    public List<YlStockArrange> getByCreateTime(String date1, String date2) {

        return stockArrangeManageMapper.getByCreateTime(date1,date2);
    }


}
